package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 广告图片管理
 * </p>
 *
 * @author author
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    @Autowired
    private IYddAdvposService iYddAdvposService;

    //根据id查询(数据回显)
    @GetMapping("/rowInfo")
    public ResultData selectById(Integer id) {
        YddAdvimg byId = iYddAdvimgService.getById(id);
        return ResultData.ok("", byId);
    }

    //分页查询
    @GetMapping("/list")
    public ResultData selectByPage(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        Page<YddAdvimg> page1 = new Page<>(page, limit);
        Page<YddAdvimgDto> dtoPage = new Page<>();

        //创建条件构造器
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        lqw.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        iYddAdvimgService.page(page1, lqw);

        //将page1的数据copy到dtoPage中
        BeanUtils.copyProperties(page1, dtoPage, "records");

        List<YddAdvimgDto> yddAdvimgDtoList = page1.getRecords().stream().map((item) -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item, yddAdvimgDto);

            YddAdvpos yddAdvpos = iYddAdvposService.getById(item.getAdvimgpos());
            if (yddAdvpos != null) {
                yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            }
            return yddAdvimgDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(yddAdvimgDtoList);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("data", dtoPage.getRecords());
        map.put("count", (int) dtoPage.getTotal());
        map.put("totalPages", (int) dtoPage.getPages());
        map.put("pageSize", dtoPage.getSize());
        map.put("currentPage", dtoPage.getCurrent());

        return ResultData.ok("", map);
    }

    //修改
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg) {
        String[] split = yddAdvimg.getAdvimgsrc().split("/");
        yddAdvimg.setAdvimgsrc("/"+split[1]);

        iYddAdvimgService.updateById(yddAdvimg);
        return R.success("200", null);
    }

    //删除
    @GetMapping("/del")
    public R delete(Integer id) {
        iYddAdvimgService.removeById(id);
        return R.success("200", null);
    }

    //添加
    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg){

        String[] split = yddAdvimg.getAdvimgsrc().split("/");
        yddAdvimg.setAdvimgsrc("/"+split[1]);

        iYddAdvimgService.save(yddAdvimg);
        return R.success("200",null);
    }
}
